package com.simdo.g768.Dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.g768.Bean.LteBand;
import com.g768.Bean.NrBand;
import com.g768.Bean.UeidBean;
import com.simdo.g768.Adapter.AutoSearchAdapter;
import com.simdo.g768.Adapter.BlackListAdapter;
import com.simdo.g768.Adapter.CellCfgAdapter;
import com.simdo.g768.Adapter.FreqResultListAdapter;
import com.simdo.g768.Adapter.HistoryAdapter;
import com.simdo.g768.Bean.ArfcnPciBean;
import com.simdo.g768.Bean.CellCfgBean;
import com.simdo.g768.Bean.HistoryBean;
import com.simdo.g768.Bean.MyUeidBean;
import com.simdo.g768.Bean.ScanArfcnBean;
import com.simdo.g768.Bean.object.TraceConfig;
import com.simdo.g768.Listener.CellClickListener;
import com.simdo.g768.Listener.ListItemListener;
import com.simdo.g768.MainActivity;
import com.simdo.g768.R;
import com.simdo.g768.ZApplication;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class TraceDialog extends Dialog {

    private final Context mContext;
    private final HistoryAdapter adapter;
    AutoCompleteTextView actv_imsi;
    boolean isAutoPci = false;
    HistoryBean historyBean;
    List<CellCfgBean> cfgList;
    CellCfgAdapter mCellCfgAdapter;

    public TraceDialog(Context context, HistoryAdapter adapter, int pos, boolean isWorking) {
        super(context, R.style.Theme_B97621);
        this.mContext = context;
        this.adapter = adapter;

        historyBean = new HistoryBean(2);
        cfgList = new ArrayList<>();
        for (int i = 0; i < 6; i++){
            cfgList.add(new CellCfgBean(i));
        }
        if (pos != -1) {
            HistoryBean checkHistory = MainActivity.getInstance().historyList.get(pos);
            isAutoPci = checkHistory.getMode() == 3;
            historyBean.setChecked(true);
            historyBean.setName(checkHistory.getName());
            historyBean.setCreateTime(checkHistory.getCreateTime());
            for (int i = 0; i < 6; i++){
                TraceConfig config = checkHistory.getConfig(i);
                historyBean.setConfig(i, config);

                int arfcn = config.getGnb_cfg().getDl_arfcn();
                if (arfcn != 0) {
                    cfgList.get(i).addData(new ArfcnPciBean(arfcn, config.getGnb_cfg().getPci()));

                    arfcn = config.getGnb_cfg().getSplit_cfg1().getDl_arfcn();
                    if (arfcn != 0) cfgList.get(i).addData(new ArfcnPciBean(arfcn, config.getGnb_cfg().getSplit_cfg1().getPci()));

                    arfcn = config.getGnb_cfg().getSplit_cfg2().getDl_arfcn();
                    if (arfcn != 0) cfgList.get(i).addData(new ArfcnPciBean(arfcn, config.getGnb_cfg().getSplit_cfg2().getPci()));
                }
            }
        }
        View view = View.inflate(context, R.layout.dialog_param_cfg, null);
        this.setContentView(view);
        this.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //this.setCancelable(false);   // 返回键不消失
        //设置dialog大小，这里是一个小赠送，模块好的控件大小设置
        Window window = this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.setNavigationBarColor(Color.parseColor("#2A72FF"));
        //window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        //StatusBarUtil.setLightStatusBar(window, true);
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);

        initView(view, pos, isWorking);
    }

    private void initView(View root, int pos, boolean isWorking) {
        root.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        root.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isWorking) dismiss();
                else checkAddOrUpdate(view, pos);
            }
        });

        root.findViewById(R.id.tv_import).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showBlackDialog();
            }
        });

        actv_imsi = root.findViewById(R.id.actv_imsi);
        if (ZApplication.getInstance().isTrace){
            AutoSearchAdapter dropImsiAdapter = new AutoSearchAdapter(mContext, MainActivity.getInstance().dropImsiList);
            actv_imsi.setAdapter(dropImsiAdapter);
            if (pos != -1) actv_imsi.setText(historyBean.getImsi());
        }else root.findViewById(R.id.group_cfg).setVisibility(View.GONE);

        RecyclerView rv_param = root.findViewById(R.id.rv_param);
        List<String> list = new ArrayList<>();
        for (List<String> bandList : MainActivity.getInstance().cellMap.values()) {
            StringBuilder sb = new StringBuilder();
            sb.append("(");
            for (String item : bandList) {
                sb.append(item).append("/");
            }
            sb.deleteCharAt(sb.length() - 1);
            sb.append(")");
            list.add(sb.toString());
        }
        mCellCfgAdapter = new CellCfgAdapter(mContext, cfgList, list, new CellClickListener() {
            @Override
            public void onClick(int pos, int type) {
                if (type == 0) showFreqListDialog(pos); // 导入
                // 1:添加按钮
                // item 点击 type 2：主  3：分裂1  4：分裂2
                else showAddDialog(pos, type, list.get(pos));
            }
        });
        rv_param.setLayoutManager(new LinearLayoutManager(mContext));
        mCellCfgAdapter.setWorking(isWorking);
        mCellCfgAdapter.setAutoPci(isAutoPci);
        rv_param.setAdapter(mCellCfgAdapter);

        final Switch switch_auto_pci = root.findViewById(R.id.switch_auto_pci);
        switch_auto_pci.setChecked(isAutoPci);
        switch_auto_pci.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isAutoPci = switch_auto_pci.isChecked();
                mCellCfgAdapter.setAutoPci(isAutoPci);
            }
        });
        if (isWorking) switch_auto_pci.setEnabled(false);
    }

    private void showAddDialog(final int pos, int type, String bandStr) {
        ArrayList<ArfcnPciBean> list = cfgList.get(pos).getList();
        int size = list.size();
        if (type == 1){ // 如果是点击添加按钮
            if (size > 2){ // 已有三个频点，不再往下执行
                MainActivity.getInstance().showToast("此小区组合已满，不支持继续添加分裂频点！");
                return;
            }
            if (pos < 2){
                if (size > 1 && list.get(0).getArfcn() > 100000){
                    MainActivity.getInstance().showToast("此小区不支持5G分裂双频点！");
                    return;
                }
            }else {
                if (size > 0 && list.get(0).getArfcn() > 100000){
                    MainActivity.getInstance().showToast("此小区不支持5G分裂频点！");
                    return;
                }

                // 3/4小区仅支持4载波   5/6小区仅支持4载波
                int count = size;
                int nextId = 2;
                switch (pos){
                    case 5:
                        nextId = 4;
                        break;
                    case 4:
                        nextId = 5;
                        break;
                    case 3:
                        nextId = 2;
                        break;
                    case 2:
                        nextId = 3;
                        break;
                }
                count += cfgList.get(nextId).getList().size();

                if (count > 3) {
                    MainActivity.getInstance().showToast("小区" + (pos + 1) + "、" + (nextId + 1) + "已配4载波，无法继续添加分裂频点！");
                    return;
                }
            }
        }

        Dialog dialog = MainActivity.getInstance().createDialog(false);

        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_param, null);

        TextView tv_cfg_title = view.findViewById(R.id.tv_cfg_title);
        TextView tv_band_str = view.findViewById(R.id.tv_band_str);
        tv_band_str.setText(MessageFormat.format("小区{0}支持频段：{1}", pos + 1, bandStr));
        final TextView btn_ok = view.findViewById(R.id.btn_ok);
        final TextView btn_cancel = view.findViewById(R.id.btn_cancel);
        TextView back = view.findViewById(R.id.back);
        final EditText ed_arfcn = view.findViewById(R.id.ed_arfcn);
        final TextView as_band = view.findViewById(R.id.as_band);

        ed_arfcn.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                while (s.toString().startsWith("0")) s.delete(0, 1);
                String string = s.toString();
                String band_str = "null";
                int band;
                if (string.length() > 5) {
                    band = NrBand.earfcn2band(Integer.parseInt(string));
                    if (band != 0) band_str = "N" + band;
                } else if (!string.isEmpty()) {
                    band = LteBand.earfcn2band(Integer.parseInt(string));
                    if (band != 0) band_str = "B" + band;
                }
                as_band.setText(MessageFormat.format("{0}{1}", mContext.getString(R.string.as_band), band_str));
            }
        });
        final EditText ed_pci = view.findViewById(R.id.ed_pci);
        if (isAutoPci) view.findViewById(R.id.group_pci).setVisibility(View.GONE);
        if (type == 1) {
            tv_cfg_title.setText(R.string.add_param);
            btn_ok.setText(R.string.add);
            btn_cancel.setText(R.string.cancel);
            btn_cancel.setTextColor(Color.parseColor("#666666"));
            back.setVisibility(View.GONE);
        } else {
            tv_cfg_title.setText(R.string.modify_param);
            btn_ok.setText(R.string.confirm_modify);
            btn_cancel.setText(R.string.delete);
            btn_cancel.setTextColor(Color.RED);
            back.setVisibility(View.VISIBLE);

            ArfcnPciBean bean = list.get(type - 2);
            ed_arfcn.setText(String.valueOf(bean.getArfcn()));
            ed_pci.setText(String.valueOf(bean.getPci()));
        }

        btn_ok.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onClick(View v) {
                String arfcnStr = ed_arfcn.getEditableText().toString().trim();
                if (arfcnStr.isEmpty()){
                    MainActivity.getInstance().showToast(mContext.getString(R.string.arfcn_empty_err));
                    return;
                }else {
                    int arfcn = Integer.parseInt(arfcnStr);
                    String newBand = as_band.getText().toString().split("：")[1];
                    if (newBand.contains("null")) {
                        MainActivity.getInstance().showToast(mContext.getString(R.string.no_as_band));
                        return;
                    }
                    int band = Integer.parseInt(newBand.substring(1));

                    String pciStr;
                    if (isAutoPci){
                        Random random = new Random();
                        pciStr = String.valueOf(random.nextInt(200) + random.nextInt(300) + 1);
                    }else pciStr = ed_pci.getEditableText().toString().trim();

                    if (pciStr.isEmpty()){
                        MainActivity.getInstance().showToast(mContext.getString(R.string.pci_empty_err));
                        return;
                    }

                    int pci = Integer.parseInt(pciStr);
                    if (!checkParam(arfcn, pci, band, type, pos)) return;

                }

                MainActivity.getInstance().closeDialog(dialog);
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btn_cancel.getText().toString().equals(mContext.getString(R.string.delete))) {
                    new AlertDialog.Builder(mContext)
                            .setTitle(R.string.delete_tip_title)
                            .setMessage(R.string.delete_tip)
                            .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    cfgList.get(pos).getList().remove(type - 2);
                                    mCellCfgAdapter.notifyItemChanged(pos);
                                    MainActivity.getInstance().closeDialog(dialog);
                                }
                            })
                            .setNegativeButton(R.string.cancel, null)
                            .show();
                } else MainActivity.getInstance().closeDialog(dialog);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.getInstance().closeDialog(dialog);
            }
        });
        MainActivity.getInstance().showDialog(dialog, view, false);
    }

    private boolean checkParam(int arfcn, int pci, int band, int type, int pos) {
        ArrayList<ArfcnPciBean> list = cfgList.get(pos).getList();
        int size = list.size();
        String bandStr;
        if (arfcn < 100000) {
            bandStr = "B" + band;
            band += 1000;
        }else {
            bandStr = "N" + band;
        }

        List<String> bandList = MainActivity.getInstance().cellMap.get(pos);
        if (bandList == null || bandList.isEmpty()){
            MainActivity.getInstance().showToast("请先设置此小区支持的频段！");
            return false;
        }
        if (!bandList.contains(bandStr)){
            StringBuilder sb = new StringBuilder();
            for (String item : bandList) {
                sb.append(item).append("/");
            }
            sb.deleteCharAt(sb.length() - 1);
            MainActivity.getInstance().showToast("小区" + (pos + 1) + "仅支持" + sb + "对应的频点！");
            return false;
        }
        switch (pos){
            case 0:
                if (!cfgList.get(1).getList().isEmpty() && band < 30){
                    int arfcn1 = cfgList.get(1).getList().get(0).getArfcn();
                    int band1 = arfcn1 > 100000 ? NrBand.earfcn2band(arfcn1) : LteBand.earfcn2band(arfcn1);
                    if (band1 < 30){
                        MainActivity.getInstance().showToast(mContext.getString(R.string.fdd_1_2));
                        return false;
                    }
                }
                break;
            case 1:
                if (!cfgList.get(0).getList().isEmpty() && band < 30){
                    int arfcn1 = cfgList.get(0).getList().get(0).getArfcn();
                    int band1 = arfcn1 > 100000 ? NrBand.earfcn2band(arfcn1) : LteBand.earfcn2band(arfcn1);
                    if (band1 < 30){
                        MainActivity.getInstance().showToast(mContext.getString(R.string.fdd_1_2));
                        return false;
                    }
                }
                break;
        }

        int maxPci = arfcn < 100000 ? 503 : 1007;
        if (pci > maxPci){
            MainActivity.getInstance().showToast(mContext.getString(arfcn < 100000 ? R.string.pci_err_tip_4g : R.string.pci_err_tip_5g));
            return false;
        }

        switch (type){
            case 1:
                for (ArfcnPciBean bean : list) {
                    int dlArfcn = bean.getArfcn();
                    if (dlArfcn == arfcn){
                        MainActivity.getInstance().showToast(mContext.getString(R.string.value_exist));
                        return false;
                    }
                    if (dlArfcn < 100000 ? LteBand.earfcn2band(dlArfcn) != LteBand.earfcn2band(arfcn) : NrBand.earfcn2band(dlArfcn) != NrBand.earfcn2band(arfcn)){
                        MainActivity.getInstance().showToast("频点" + dlArfcn + "与" + arfcn + "不同频段，请修改！");
                        return false;
                    }
                }
                list.add(new ArfcnPciBean(arfcn, pci));
                break;
            case 2:
                if (size > 1){
                    int dlArfcn = list.get(1).getArfcn();
                    // 检查是否和其他存在相同频点
                    if (dlArfcn == arfcn || (size > 2 && list.get(2).getArfcn() == arfcn)){
                        MainActivity.getInstance().showToast(mContext.getString(R.string.value_exist));
                        return false;
                    }
                    // 检查和当前索引是否完全一样
                    if (arfcn == list.get(0).getArfcn() && pci == list.get(0).getPci()){
                        MainActivity.getInstance().showToast(mContext.getString(R.string.no_modify));
                        return false;
                    }
                    boolean difLen = String.valueOf(dlArfcn).length() != String.valueOf(arfcn).length(); // 不同长度的频点
                    if (difLen || (dlArfcn < 100000 ? LteBand.earfcn2band(dlArfcn) != LteBand.earfcn2band(arfcn) : NrBand.earfcn2band(dlArfcn) != NrBand.earfcn2band(arfcn)) ){
                        MainActivity.getInstance().showToast("分裂频点" + dlArfcn + "与" + arfcn + "不同频段，请修改！");
                        return false;
                    }
                }
                list.get(0).setArfcn(arfcn);
                list.get(0).setPci(pci);
                break;
            default:
                int dlArfcn = list.get(0).getArfcn();
                // 检查是否和其他存在相同频点
                if (dlArfcn == arfcn || (size > 2 && list.get(type == 3 ? 2 : 1).getArfcn() == arfcn)){
                    MainActivity.getInstance().showToast(mContext.getString(R.string.value_exist));
                    return false;
                }
                int index = type - 2;
                // 检查和当前索引是否完全一样
                if (arfcn == list.get(index).getArfcn() && pci == list.get(index).getPci()){
                    MainActivity.getInstance().showToast(mContext.getString(R.string.no_modify));
                    return false;
                }
                boolean difLen = String.valueOf(dlArfcn).length() != String.valueOf(arfcn).length(); // 不同长度的频点
                if (difLen || (dlArfcn < 100000 ? LteBand.earfcn2band(dlArfcn) != LteBand.earfcn2band(arfcn) : NrBand.earfcn2band(dlArfcn) != NrBand.earfcn2band(arfcn)) ){
                    MainActivity.getInstance().showToast("主频点" + dlArfcn + "与" + arfcn + "不同频段，请修改！");
                    return false;
                }

                list.get(index).setArfcn(arfcn);
                list.get(index).setPci(pci);
                break;
        }
        mCellCfgAdapter.notifyItemChanged(pos);
        return true;
    }

    private void checkAddOrUpdate(View view, int pos){
        if (ZApplication.getInstance().isTrace){
            String imsi = actv_imsi.getText().toString().trim();
            if (imsi.isEmpty()){
                MainActivity.getInstance().showToast(mContext.getString(R.string.imsi_err_tip));
                return;
            }
            ArrayList<UeidBean> imsiList = new ArrayList<>();
            imsiList.add(new UeidBean(imsi, ""));
            for (int i = 0; i < 6; i++){
                historyBean.getConfig(i).getTrace_cfg().setImsi_num(1);
                historyBean.getConfig(i).getTrace_cfg().setImsi_list(imsiList);
            }
            MainActivity.getInstance().addDropImsiList(imsi);
        }
        historyBean.setMode(isAutoPci ? 3 : 2);
        for (int i = 0; i < 6; i++){
            resetCellConfig(i);
            ArrayList<ArfcnPciBean> list = cfgList.get(i).getList();
            switch (list.size()){
                case 1:
                    historyBean.getConfig(i).getGnb_cfg().setDl_arfcn(list.get(0).getArfcn());
                    historyBean.getConfig(i).getGnb_cfg().setPci(list.get(0).getPci());
                    break;
                case 2:
                    historyBean.getConfig(i).getGnb_cfg().setDl_arfcn(list.get(0).getArfcn());
                    historyBean.getConfig(i).getGnb_cfg().setPci(list.get(0).getPci());

                    historyBean.getConfig(i).getGnb_cfg().getSplit_cfg1().setDl_arfcn(list.get(1).getArfcn());
                    historyBean.getConfig(i).getGnb_cfg().getSplit_cfg1().setPci(list.get(1).getPci());
                    break;
                case 3:
                    historyBean.getConfig(i).getGnb_cfg().setDl_arfcn(list.get(0).getArfcn());
                    historyBean.getConfig(i).getGnb_cfg().setPci(list.get(0).getPci());

                    historyBean.getConfig(i).getGnb_cfg().getSplit_cfg1().setDl_arfcn(list.get(1).getArfcn());
                    historyBean.getConfig(i).getGnb_cfg().getSplit_cfg1().setPci(list.get(1).getPci());

                    historyBean.getConfig(i).getGnb_cfg().getSplit_cfg2().setDl_arfcn(list.get(2).getArfcn());
                    historyBean.getConfig(i).getGnb_cfg().getSplit_cfg2().setPci(list.get(2).getPci());
                    break;
            }
        }
        if (pos != -1){
            adapter.updateData(pos, historyBean);
        } else if (!adapter.addData(historyBean)) {
            MainActivity.getInstance().showToast(mContext.getString(R.string.value_exist));
            return;
        }
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        MainActivity.getInstance().showToast(pos != -1 ? mContext.getString(R.string.updated) : mContext.getString(R.string.added), true);
        dismiss();
    }

    private void resetCellConfig(int cell) {
        historyBean.getConfig(cell).getGnb_cfg().setDl_arfcn(0);
        historyBean.getConfig(cell).getGnb_cfg().setPci(0);

        historyBean.getConfig(cell).getGnb_cfg().getSplit_cfg1().setDl_arfcn(0);
        historyBean.getConfig(cell).getGnb_cfg().getSplit_cfg1().setPci(0);

        historyBean.getConfig(cell).getGnb_cfg().getSplit_cfg2().setDl_arfcn(0);
        historyBean.getConfig(cell).getGnb_cfg().getSplit_cfg2().setPci(0);
    }

    private void showBlackDialog() {
        Dialog dialog = MainActivity.getInstance().createDialog(false);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_blacklist_silde, null);

        RecyclerView black_list = view.findViewById(R.id.black_list);
        black_list.setLayoutManager(new LinearLayoutManager(mContext));
        List<MyUeidBean> blackList = MainActivity.getInstance().getBlackList();
        BlackListAdapter blackListAdapter = new BlackListAdapter(mContext, blackList, new ListItemListener() {
            @Override
            public void onItemClickListener(int position) {
                actv_imsi.setText(blackList.get(position).getUeidBean().getImsi());
                MainActivity.getInstance().closeDialog(dialog);
            }
        });
        black_list.setAdapter(blackListAdapter);
        view.findViewById(R.id.tv_left_tip).setVisibility(View.GONE);
        view.findViewById(R.id.tv_add).setVisibility(View.GONE);
        view.findViewById(R.id.tv_import).setVisibility(View.GONE);
        view.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.getInstance().closeDialog(dialog);
            }
        });
        MainActivity.getInstance().showDialog(dialog, view, false, true);
    }

    private void showFreqListDialog(int cell) {
        if (getFreqList() == null) {
            MainActivity.getInstance().showToast(mContext.getString(R.string.no_freq_data_tip));
            return;
        }
        Dialog dialog = MainActivity.getInstance().createDialog(false);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_freqlist_silde, null);

        view.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity.getInstance().closeDialog(dialog);
            }
        });

        RecyclerView freq_list = view.findViewById(R.id.freq_list);
        freq_list.setLayoutManager(new LinearLayoutManager(mContext));

        FreqResultListAdapter adapter = new FreqResultListAdapter(mContext, getFreqList(), new ListItemListener() {
            @Override
            public void onItemClickListener(int position) {
                if (importArfcn(getFreqList().get(position), 0, cell)) MainActivity.getInstance().closeDialog(dialog);
                //else MainActivity.getInstance().showToast(mContext.getString(R.string.cell_not_support_tip));
            }
        });
        freq_list.setLayoutManager(new LinearLayoutManager(mContext));
        freq_list.setAdapter(adapter);
        MainActivity.getInstance().showDialog(dialog, view, false, true);
    }

    private LinkedList<ScanArfcnBean> getFreqList() {
        return MainActivity.getInstance().getFreqList();
    }

    public boolean importArfcn(ScanArfcnBean bean, int inPci, int cell) {
        int band;
        int arfcn = bean.getDl_arfcn();
        int pci = bean.getPci();
        if (arfcn < 100000) {
            band = LteBand.earfcn2band(arfcn);
            // 4G PCI 算法： 公网pci % 3，取余 +- 1，如果为 0则+1， 2则-1， 1则+-1都可以
            if (pci % 3 == 2) pci -= 1;
            else pci += 1;
        } else {
            band = NrBand.earfcn2band(arfcn);
            // 5G PCI 算法：
            // 若在方法调用传进来的pci不为0， 表示做处理了，这里直接取用。
            // 否则走 % 3 取余逻辑（若发现和公网有冲突，也不理了，谁让你特意选这么个玩意进来）
            if (inPci != 0) pci = inPci;
            else {
                if (pci % 3 == 2) pci -= 1;
                else pci += 1;
            }
        }
        return checkParam(arfcn, pci, band, 1, cell);
    }
}
